package com.zql.LeetCode;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created By ShuHua on 2021/9/29.
 */
public class MinWindow_76 {
    public static String minWindow(String s,String t){
        if(s.toCharArray().length<t.toCharArray().length)return "";
        Map<Character,Integer> need = new HashMap<Character, Integer>();
        Map<Character,Integer> window = new HashMap<Character, Integer>();

        for(char c:t.toCharArray()){
            if(need.containsKey(c)){
                need.put(c,need.get(c)+1);
            }else {
                need.put(c,1);
            }
        }

        int left=0;
        int right=0;
        int start=0;
        int valid=0;
        int len = s.length();
        char[] s1= s.toCharArray();
        while (right<s1.length){
            //扩大窗口，往widow加数据
            char c = s1[right];

            if(window.containsKey(c)){
                window.put(c,window.get(c)+1);
            }else {
                window.put(c, 1);
            }
            if(need.containsKey(c) && window.get(c).equals(need.get(c))){
                valid++;
            }
            //判断window中的数据是否满足need

            if(valid==need.size()){
                //停止扩大，开始缩小left
                while(valid==need.size()){
                    int n =window.get(s1[left]);
                    if(n==1){
                        window.remove(s1[left]);
                    }else if(n>1){
                        window.put(s1[left],n-1);
                    }
                    if(need.containsKey(s1[left])&&(!window.containsKey(s1[left])||need.get(s1[left])>window.get(s1[left]))){
                        valid--;
                    }
                    left++;
                }
                int curLen =right-left+1;
                if(curLen<len){
                    len=curLen;
                    start=left-1;
                }

            }
            right++;
        }
        return len==s.length() ? "":s.substring(start,start+len+1);
    }
/**
    string minWindow(string s, string t) {
        unordered_map<char, int> need, window;
        for (char c : t) need[c]++;

        int left = 0, right = 0;
        int valid = 0;
        // 记录最小覆盖子串的起始索引及长度
        int start = 0, len = INT_MAX;
        while (right < s.size()) {
            // c 是将移入窗口的字符
            char c = s[right];
            // 右移窗口
            right++;
            // 进行窗口内数据的一系列更新
            if (need.count(c)) {
                window[c]++;
                if (window[c] == need[c])
                    valid++;
            }

            // 判断左侧窗口是否要收缩
            while (valid == need.size()) {
                // 在这里更新最小覆盖子串
                if (right - left < len) {
                    start = left;
                    len = right - left;
                }
                // d 是将移出窗口的字符
                char d = s[left];
                // 左移窗口
                left++;
                // 进行窗口内数据的一系列更新
                if (need.count(d)) {
                    if (window[d] == need[d])
                        valid--;
                    window[d]--;
                }
            }
        }
        // 返回最小覆盖子串
        return len == INT_MAX ?
                "" : s.substr(start, len);
    }
*/
    public static String minWindow1(String s,String t){
        Map<Character,Integer> need = new HashMap<Character, Integer>();
        Map<Character,Integer> window = new HashMap<Character, Integer>();

        for(char c:t.toCharArray()){
            if(need.containsKey(c)){
                need.put(c,need.get(c)+1);
            }else {
                need.put(c,1);
            }
        }

        int left = 0, right = 0;
        int valid = 0;
        // 记录最小覆盖子串的起始索引及长度
        int start = 0, len = Integer.MAX_VALUE;//len的初始值设成最大值方便一点 如果设置成s的长度，那出现s:aa,t:aa这种情况会返回空
        char[] s1 = s.toCharArray();
        while (right < s.length()) {
            char c = s1[right];
            // 右移窗口
            right++;
            // 进行窗口内数据的一系列更新
            if (need.containsKey(c)) {
                if(window.containsKey(c)){
                    window.put(c,window.get(c)+1);
                }else {
                    window.put(c,1);
                }
                if (window.get(c).equals(need.get(c)))
                    valid++;
            }
            // 判断左侧窗口是否要收缩
            while (valid == need.size()) {
                // 在这里更新最小覆盖子串
                if (right - left < len) {
                    start = left;
                    len = right - left;
                }
                // d 是将移出窗口的字符
                char d = s1[left];
                // 左移窗口
                left++;
                // 进行窗口内数据的一系列更新
                if (need.containsKey(d)) {
                    if (window.get(d).equals(need.get(d)))
                        valid--;
                    if(window.get(d)<=1){
                        window.remove(d);
                    }else {
                        window.put(d,window.get(d)-1);
                    }
                }
            }
        }
        // 返回最小覆盖子串
        return len == Integer.MAX_VALUE ?
                "" : s.substring(start, start+len);

    }

    public static String minwindow2(String s, String t){
        Map<Character,Integer> need = new HashMap<Character, Integer>();
        Map<Character,Integer> window = new HashMap<Character, Integer>();

        for(char c : t.toCharArray()){
            if(need.containsKey(c)){
                need.put(c,need.get(c)+1);
            }else {
                need.put(c,1);
            }
        }

        int start =0;
        int len = Integer.MAX_VALUE;
        int valid =0;
        int right =0;
        int left =0;
        char[]s1 = s.toCharArray();
        while (right<s.length()){
            char c = s1[right];
            right++;

            if(need.containsKey(c)){
                if(window.containsKey(c)){
                    window.put(c,window.get(c)+1);
                }else {
                    window.put(c,1);
                }
                if(need.get(c).equals(window.get(c))){
                    valid++;
                }
            }

            while(valid==need.size()){
                if(right-left<len){
                    start=left;
                    len=right-left;
                }
                char c1 = s1[left];
                left++;

                if(need.containsKey(c1)){
                    if(need.get(c1).equals(window.get(c1))){
                        valid--;
                    }
                    if (window.get(c1)>1){
                        window.put(c1,window.get(c1)-1);
                    }else {
                        window.remove(c1);
                    }

                }

            }
        }
        return len==Integer.MAX_VALUE ? "":s.substring(start,start+len);

    }

    public static void main(String[] args) {
//        System.out.println(minWindow("aa","aa"));
//        System.out.println(minWindow1("ADOBECODEBANC","ABC"));
        String s= "ADOBECODEBANC";
        System.out.println(minwindow2("a","a"));
        System.out.println(minwindow2(s,"ABC"));
    }
}
